package org.daochong.fastdev;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class FunctionFactory implements IFunctionFactory, ApplicationContextAware {

	private ApplicationContext applicationContext;

	private List<Function> functions;

	public Function getFunction(String id) {
		for (Function function : getFunctions()) {
			if (function.is(id))
				return function;
		}
		return null;
	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	private List<Function> getFunctions() {
		if (this.functions == null) {
			this.functions = new ArrayList<Function>();
			this.functions.addAll(this.applicationContext.getBeansOfType(Function.class).values());
		}
		return this.functions;
	}
}
